package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.entity.CarInformation;
import com.yaojiaxun.logisticsPlatformSystem.service.AreaService;
import com.yaojiaxun.logisticsPlatformSystem.service.CarInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/15 12:58
 * @Description:
 */
@Controller
public class CarInformationController {

    @Autowired
    private CarInformationService carInformationService;

    @Autowired
    private AreaService areaService;

    @RequestMapping("/car_manage")
    public String carManageIndex(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        map.put("pageDTO", carInformationService.findPage(page));
        return "system/car/car_manage";
    }

    @RequestMapping("/carDelete")
    public String deleteCarById(@RequestParam("carId") Integer carId, @RequestParam("page") Integer page){
        carInformationService.deleteCar(carId);
        return "redirect:/car_manage?page="+page;
    }

    @RequestMapping("/car_detail")
    public String getDetail(@RequestParam("carId") Integer carId, @RequestParam("page") Integer page, Map<String, Object> map){
        map.put("page", page);
        map.put("carInfo", carInformationService.findCarByCarId(carId));
        map.put("provinces", areaService.findAllProvinces());
        map.put("cities", areaService.findAllCities());
        map.put("countries", areaService.findAllCountries());
        return "system/car/car_detail";
    }

    @RequestMapping("/car_update_after")
    public String updateCarAfter(@RequestParam("page") Integer page, CarInformation carInformation){
        carInformationService.updateCarByCarId(carInformation);
        return "redirect:/car_manage?page="+page;
    }

    @RequestMapping("/carAddBefore")
    public String addCarBefore(){
        return "system/car/car_add";
    }

    @RequestMapping("/car_add_after")
    public String addCarAfter(CarInformation carInformation){
        carInformationService.addCarInformation(carInformation);
        return "redirect:/car_manage";
    }

    @RequestMapping("/findCarByCarNumber")
    public String findCarByCarNumber(@RequestParam("carNumber") String carNumber,Map<String, Object> map){
        map.put("results", carInformationService.findCarByCarNumber(carNumber));
        return "system/car/car_search_results";
    }
}
